package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.personinfoDao;
import com.xueyu.domain.personinfo.personinfoBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "personinfo")
public class  personinfoController {
    //查询
    @Autowired
    personinfoDao personinfoDao;

    @RequestMapping("/add")
    public ResultBean add(@RequestBody personinfoBean personinfoBean,
                          @RequestParam(value = "fileList", required = false) MultipartFile fileList)throws Exception{
        ResultBean ResultBean= new ResultBean();
        if (personinfoBean.getPersonId()==0){
            personinfoDao.insert(personinfoBean);
        }else{
            personinfoDao.updateById(personinfoBean);
        }
        return ResultBean;
    }
    @RequestMapping("/del/{PersonId}")
    public ResultBean del(@PathVariable("PersonId") int PersonId){
        personinfoDao.deleteById(PersonId);
        return  new ResultBean();
    }
    @RequestMapping("/getid/{PersonId}")
    public Map<String, Object> getId(@PathVariable("PersonId") int PersonId){
        ResultBean resultBean = new ResultBean();
        if (PersonId>0) {
            QueryWrapper<personinfoBean> queryWrapper = Wrappers.query();
            queryWrapper.eq("PersonId", PersonId);
            List<personinfoBean> personinfoList = personinfoDao.selectList(queryWrapper);
            resultBean.put("data", personinfoList);
        }
        return resultBean;
    }
    @RequestMapping("/fy")
    public ResultBean fy(@RequestBody Map<String, Object> map) throws ParseException {
        int current = 0;
        int size = 0;
        if (map.containsKey("Current") && map.get("Current") instanceof Integer) {
            current = (Integer) map.get("Current");
        }
        if (map.containsKey("size") && map.get("size") instanceof Integer) {
            size = (Integer) map.get("size");
        }
        Page<personinfoBean> page = new Page<>(current,size);
        QueryWrapper<personinfoBean> queryWrapper = createSchoolQueryWrapper(map);
        page = personinfoDao.selectPage(page,queryWrapper);
        List<personinfoBean> list = page.getRecords();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ResultBean resultBean = new ResultBean();
        for (int i = 0; i<list.size(); i++){
            sdf.parse(list.get(i).getInTime());
            sdf.parse(list.get(i).getExpireDate());
            List<personinfoBean> personinfoBeanList = list;
            System.out.println("personinfoBeanList："+ personinfoBeanList);
            resultBean.put("list",personinfoBeanList);
        }
        resultBean.put("page",page);
        return resultBean;
    }

    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody Map map){
        QueryWrapper<personinfoBean> queryWrapper = createSchoolQueryWrapper(map);
        List<personinfoBean> list = personinfoDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }
    private QueryWrapper<personinfoBean> createSchoolQueryWrapper(Map map) {
        String personId=(String) map.get("personId");
        String communityId=(String) map.get("communityId");
        String houseId=(String) map.get("houseId");
        String bedId=(String) map.get("bedId");
        QueryWrapper<personinfoBean> queryWrapper = Wrappers.query();
        if (personId != null && !"".equals(personId)) {
            queryWrapper.eq("personId", personId);
        }
        if (communityId != null && !"".equals(communityId)) {
            queryWrapper.eq("communityId", communityId);
        }
        if (houseId != null && !"".equals(houseId)) {
            queryWrapper.eq("houseId", houseId);
        }
        if (bedId != null && !"".equals(bedId)) {
            queryWrapper.eq("bedId", bedId);
        }
        return queryWrapper;
    }
}